const router = require('koa-router')()
const {login} = require('../controller/user')
const {SuccessModel,ErrorModel} = require('../model/resModel.js')

router.prefix('/api/user')

router.post('/login', async (ctx, next) => {
    const {username,password} = ctx.request.body
    const data = await login(username,password)
    if(data.username){
        // 设置 session
       ctx.session.username = data.username
       ctx.session.realName = data.realname

        ctx.body = new SuccessModel()
        return 
    }
    ctx.body=new ErrorModel('登录失败')

});

router.get('/login-test', async (ctx,next)=>{
    if(ctx.session.username){
        ctx.body = {
            errno:0,
            msg:'登陆成功'
        }
        return
    }
    ctx.body = {
        errno:-1,
        msg:'未登录'
    }
})

module.exports = router
